import { message } from 'antd'
import { reqGetRealSpeed, reqOneVehicle, reqGetLatestPosition, reqGetStatusDataRead, reqOpenOrRealDataRead} from './service'
export default {
  namespace: 'dataCollection',
  state: {
    vehicleInfo: {},
    status: false,
    postionData:[],
    speedData:[],
  },
  reducers: {
    setVehicle(state, { payload }) {
      return { ...state, vehicleInfo: payload}
    },
    setStatus(state, { payload }) {
      return { ...state, status: payload}
    },
    setPosition(state, { payload }) {
      return { ...state, postionData: payload}
    },
    setSpeed(state, { payload }) {
      return { ...state, speedData: payload}
    }
  },
  effects: {
    *fetchOne({ payload }, { call, put }) {
      const res = yield call(reqOneVehicle, payload)
      try {
        if (res.code === 0) {
          yield put({
            type: 'setVehicle',
            payload: res.data,
          })
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqGetStatusDataRead({ payload }, { call, put }) {
      const res = yield call(reqGetStatusDataRead, payload)
      try {
        if (res.code === 9) {
          yield put({
            type: 'setStatus',
            payload: res.data,
          })
        }
      } catch (error) {
        message.error(error)
      }
    },
    *reqOpenOrRealDataRead({ payload }, { call, put }) {
      const url = 'https://iot-pre.diwork.com/reader/api/v1/realTimeDataByTag?tags=cars@%E7%BB%8F%E5%BA%A6-14,cars@%E7%BB%8F%E5%BA%A6-13,cars@%E7%BB%8F%E5%BA%A6-12,cars@%E7%BB%8F%E5%BA%A6-11,cars@%E7%BB%8F%E5%BA%A6-10,cars@%E7%BB%8F%E5%BA%A6-9,cars@%E7%BB%8F%E5%BA%A6-8,cars@%E7%BB%8F%E5%BA%A6-7,cars@%E7%BB%8F%E5%BA%A6-6,cars@%E7%BB%8F%E5%BA%A6-5,cars@%E7%BB%8F%E5%BA%A6-4,cars@%E7%BB%8F%E5%BA%A6-3,cars@%E7%BA%AC%E5%BA%A6-14,cars@%E7%BA%AC%E5%BA%A6-13,cars@%E7%BA%AC%E5%BA%A6-12,cars@%E7%BA%AC%E5%BA%A6-11,cars@%E7%BA%AC%E5%BA%A6-10,cars@%E7%BA%AC%E5%BA%A6-9,cars@%E7%BA%AC%E5%BA%A6-8,cars@%E7%BA%AC%E5%BA%A6-7,cars@%E7%BA%AC%E5%BA%A6-6,cars@%E7%BA%AC%E5%BA%A6-5,cars@%E7%BA%AC%E5%BA%A6-4,cars@%E7%BA%AC%E5%BA%A6-3,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-14,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-13,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-12,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-11,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-10,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-9,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-8,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-7,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-6,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-5,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-4,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-3,cars@%E6%B8%A9%E5%BA%A6-14,cars@%E6%B8%A9%E5%BA%A6-13,cars@%E6%B8%A9%E5%BA%A6-12,cars@%E6%B8%A9%E5%BA%A6-11,cars@%E6%B8%A9%E5%BA%A6-10,cars@%E6%B8%A9%E5%BA%A6-9,cars@%E6%B8%A9%E5%BA%A6-8,cars@%E6%B8%A9%E5%BA%A6-7,cars@%E6%B8%A9%E5%BA%A6-6,cars@%E6%B8%A9%E5%BA%A6-5,cars@%E6%B8%A9%E5%BA%A6-4,cars@%E6%B8%A9%E5%BA%A6-3,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-14,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-13,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-12,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-11,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-10,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-9,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-8,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-7,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-6,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-5,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-4,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-3,cars@%E8%BD%AC%E9%80%9F-14,cars@%E8%BD%AC%E9%80%9F-13,cars@%E8%BD%AC%E9%80%9F-12,cars@%E8%BD%AC%E9%80%9F-11,cars@%E8%BD%AC%E9%80%9F-10,cars@%E8%BD%AC%E9%80%9F-9,cars@%E8%BD%AC%E9%80%9F-8,cars@%E8%BD%AC%E9%80%9F-7,cars@%E8%BD%AC%E9%80%9F-6,cars@%E8%BD%AC%E9%80%9F-5,cars@%E8%BD%AC%E9%80%9F-4,cars@%E8%BD%AC%E9%80%9F-3,cars@%E9%80%9F%E5%BA%A6-14,cars@%E9%80%9F%E5%BA%A6-13,cars@%E9%80%9F%E5%BA%A6-12,cars@%E9%80%9F%E5%BA%A6-11,cars@%E9%80%9F%E5%BA%A6-10,cars@%E9%80%9F%E5%BA%A6-9,cars@%E9%80%9F%E5%BA%A6-8,cars@%E9%80%9F%E5%BA%A6-7,cars@%E9%80%9F%E5%BA%A6-6,cars@%E9%80%9F%E5%BA%A6-5,cars@%E9%80%9F%E5%BA%A6-4,cars@%E9%80%9F%E5%BA%A6-3,cars@%E7%BB%8F%E5%BA%A6-2,cars@%E7%BA%AC%E5%BA%A6-2,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-2,cars@%E6%B8%A9%E5%BA%A6-2,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-2,cars@%E8%BD%AC%E9%80%9F-2,cars@%E9%80%9F%E5%BA%A6-2,cars@%E7%BB%8F%E5%BA%A6-1,cars@%E7%BA%AC%E5%BA%A6-1,cars@%E5%80%BE%E6%96%9C%E5%BA%A6-1,cars@%E6%B8%A9%E5%BA%A6-1,cars@%E7%87%83%E6%B2%B9%E4%BD%99%E9%87%8F-1,cars@%E8%BD%AC%E9%80%9F-1,cars@%E9%80%9F%E5%BA%A6-1';
      const cookie = '_WorkbenchCross_=Ultraman; at=cc83dce1-a9e1-42c8-b47b-bcdb72c2ccc3; yonyou_uid=8620ea97-e454-4b6b-825d-11a0e18be45d; yonyou_uname=13308239129; yht_username_diwork=ST-10752-6o7GSSAls5EnowhTKS4I-user-y3me-pre.diwork.com__8620ea97-e454-4b6b-825d-11a0e18be45d; yht_usertoken_diwork=yEWSsCm85oUbYQVsatN%2BHtSlPwkys1BTmNghcCDXHqG3J9ALBJqylizf7q3OXDSVsNb4loK8wOy8jHu5gx0pSw%3D%3D; yht_access_token=bttbzJMV3JUeUZLMUxCVkJtc1Q2UWo0VE9mOHkxSVo4Q0ZkbmpZVnZoSEVUakN6OG05b1N6TFpoRFppWUxMSzJXSkFXeXB5dkk5cy9EQ1dDUXJEejFuNFBUTlEremhpcDF1N09YQkdRaXA1VENIUUJEN1c4OFF6cmlCY1RpYmhpbHpfX3VzZXIteTNtZS1wcmUuZGl3b3JrLmNvbQ..__61ddc453cb427a1f090c4d9d8efacc4f_1631574777489; wb_at=LMjnmtrojsnt9LL3krr7mnvgMDLqBjtrdqjxpldjoqdkchvnqjkbnl; PHPSESSID=pqogst65ej134fh6nfohjjop4a; ck_safe_chaoke_csrf_token=4de8a1cfcc9f111d4070833737d8a1eb; YKJ_IS_DIWORK=1; YKJ_DIWORK_DATA=%7B%22data%22%3A%7B%22is_diwork%22%3A1%2C%22cur_qzid%22%3A%2222950%22%7D%2C%22key%22%3A%22ea42b5e11d1dbc78ecfca47d36d5a3c9%22%7D; mdd_monitor_uid=; mdd_monitor_record=false';
      const vehicleName = 'cars';
      const interval = 1000;
      const vehicleIdList = new Array(payload.vehicleIdList)
      console.log(vehicleIdList)
      const formData = {
        url,
        cookie,
        vehicleName,
        interval,
        vehicleIdList:[...vehicleIdList],
      }
      const res = yield call(reqOpenOrRealDataRead, formData)
      try {
        if (res.code === 0) {
          message.success(res.msg)
        }
      } catch (error) {
        message.error(error)
      }
    },
    *reqGetLatestPosition({ payload }, { call, put }) {
      const res = yield call(reqGetLatestPosition, payload)
      try {
        if (res.code === 0) {
          yield put({
            type: 'setPosition',
            payload: res.data,
          })
        }
      } catch (error) {
        message.error(error)
      }
    },
    *reqGetRealSpeed({ payload }, { call, put }) {
      const res = yield call(reqGetRealSpeed, payload)
      try {
        if (res.code === 0) {
          yield put({
            type: 'setSpeed',
            payload: res.data,
          })
        }
      } catch (error) {
        message.error(error)
      }
    }
  }
}